class MyofficeController < ApplicationController
before_filter :checkclockout


def login
  if request.post?
        if session[:employee] = Employee.authenticate(params[:email], params[:password])
          session[:employee_id] = session[:employee].id
		  session[:employee_name] = session[:employee].name
		  session[:employee_level] = session[:employee].level_id
		  session[:employee_mail] = session[:employee].email
          
          #checkin
          @check_employee_attendance = Attendance.find(:first,:conditions => ["employee_id = ? and clock_in_date = ?", 
            session[:employee_id],Date.today])
          
          #if never login for today automatically clock in for today
          if @check_employee_attendance.nil?
          @attendance = Attendance.new
          @attendance.employee_id = session[:employee_id]
          @attendance.clock_in_date = Date.today
          @attendance.clock_in_time = Time.now
		  @attendance.year = Time.now.year
		  @attendance.month = Time.now.month
          @attendance.save
          end
          redirect_to(:action => 'index')
        else
        flash[:notice] = 'Sorry, please try again'
        end
  end
end


def logout
session[:employee] = nil
session[:employee_id] = nil
session[:employee_name] = nil
session[:employee_level] = nil
#flash[:notice] = 'Logout'
redirect_to(:action => 'index')
end

def index
@notice_boards = NoticeBoard.find(:all,:order => 'created_at desc', :limit => 3)
@cicaks = days_in_month(Time.now.year, Time.now.month)
@visitors = Visitor.find(:all,:order => 'created_at desc')
@meetings = Meeting.find(:all)
if session[:employee].nil?
render(:layout => 'front')
end
end

def clock_out
  @attendance = Attendance.find(:first,:conditions => ["employee_id = ? and clock_in_date = ? and clock_out_date is null and clock_out_time is null", session[:employee_id],Date.today])
  if !@attendance.nil?
  @attendance.employee_id = session[:employee_id]
  @attendance.clock_out_date = Date.today
  @attendance.clock_out_time = Time.now
  @attendance.save
  redirect_to(:action => 'index')
else
  flash[:notice] = 'Sorry, you already clock out'
redirect_to(:action => 'index')
end
end

def apply_leave
@employee = Employee.find(session[:employee_id])
@leave_types = LeaveType.find(:all)
@leave = Leave.new(params[:leave])
@leave_portions = LeavePortion.find(:all)
end


def create_leave
@employee = Employee.find(session[:employee_id])
@leave_types = LeaveType.find(:all)
@leave_portions = LeavePortion.find(:all)
@leave = Leave.new(params[:leave])
@leave.employee_id = session[:employee_id]
@leave.leave_status_id = 1
if @leave.save
	#extract generate 1 by 1 field
	#find compare date
	@a = @leave.start_date
	@b = @leave.end_date
	(@a..@b).to_a.each do |l|
	@leave_detail = LeaveDetail.new
	@leave_detail.leave_id = @leave.id
	@leave_detail.date = l
	@leave_detail.year = l.year
	@leave_detail.month = l.month
	@leave_detail.employee_id = session[:employee_id]
	@leave_detail.save
	end
redirect_to(:action => 'my_leave_information')
else
  render(:action => 'apply_leave')
end
end

def out_of_office_information
@leave_offices = LeaveOffice.find(:all)
end


def inform_out_office
@leave_office = LeaveOffice.new(params[:leave_office])
@employee = Employee.find(session[:employee_id])
end

def create_leave_office
@leave_office = LeaveOffice.new(params[:leave_office])
@leave_office.employee_id = session[:employee_id]
if @leave_office.save
  redirect_to(:action => 'out_of_office_information')
else
  render(:action => 'inform_out_office')
end
end

def my_leave_information
@employee = Employee.find(session[:employee_id])
@leave_types = LeaveType.find(:all)
#@leave_details = LeaveDetail.find(:all)
@leave_details = LeaveDetail.paginate :all,:conditions => ["employee_id = ?", session[:employee_id]], :page => params[:page], :per_page => 35
@usertotalleave = LeaveDetail.count(:id,:conditions => ["employee_id = ? and year = ?",session[:employee_id], Time.now.year])
if @employee.gender_id == 1
@totalleave = LeaveType.sum(:total,:conditions => ["id <> 5"])
else
@totalleave = LeaveType.sum(:total,:conditions => ["id <> 6"])
end
end


def leave_information
@leave_types = LeaveType.find(:all)
#@leave_details = LeaveDetail.find(:all)
@leaves = Leave.paginate :all, :page => params[:page], :per_page => 35
end


def show_leave_info
@leave_types = LeaveType.find(:all)
@leave_type = LeaveType.find(params[:id])
@leaves = Leave.paginate :all, :conditions => ["leave_type_id = ?",@leave_type.id],:page => params[:page], :per_page => 35
end


def show_employe_leave
@employee = Employee.find(params[:id])
@leave_types = LeaveType.find(:all)
@leave_details = LeaveDetail.paginate :all,:conditions => ["employee_id = ?", params[:id]], :page => params[:page], :per_page => 35
@usertotalleave = LeaveDetail.count(:id,:conditions => ["employee_id = ? and year = ?",params[:id], Time.now.year])
if @employee.gender_id == 1
@totalleave = LeaveType.sum(:total,:conditions => ["id <> 5"])
else
@totalleave = LeaveType.sum(:total,:conditions => ["id <> 6"])
end
end


def late_information
#@attendances = Attendance.find(:all,:conditions => [""]:include => [{:employee => :attendance_working_hour}])
@attendances = Attendance.find(:all, :conditions => ["attendance_working_hours.start_time <= attendances.clock_in_time"],:include => [{:employee => :attendance_working_hour}],:order => 'employees.name',:group => 'employees.name')
end

def show_late
@employee = Employee.find(params[:id])
@attendances = Attendance.find(:all,:conditions => ["clock_in_time >= ? and employee_id = ?",@employee.attendance_working_hour.start_time,@employee.id],:include => [{:employee => :attendance_working_hour}],:order => 'employees.name')
end


def attendance_information
@attendances = Attendance.find(:all,:conditions => ["year = ? and month = ? and employee_id = ?",Time.now.year, Time.now.month,session[:employee_id]])
@days = days_in_month(Time.now.year, Time.now.month)
end

def show_month_attendance
@month = params[:id].to_i
@year = params[:year].to_i
@attendances = Attendance.find(:all,:conditions => ["year = ? and month = ? and employee_id = ?",params[:year], params[:id],session[:employee_id]])
@days = days_in_month(@year, @month)
end

def read_notice_board
@notice_board = NoticeBoard.find(params[:id])
end


def notice_board_list
@notice_boards = NoticeBoard.find(:all,:order => 'created_at desc')
end


def directory
@employees = Employee.find(:all)
end

def show_employee
@employee = Employee.find(params[:id])
@location = GeoKit::Geocoders::MultiGeocoder.geocode(@employee.address+','+@employee.postcode+','+@employee.postcode)
end

def calendar
  @month = (params[:month] || Time.zone.now.month).to_i
    @year = (params[:year] || Time.zone.now.year).to_i

    @shown_month = Date.civil(@year, @month)

    @event_strips = Event.event_strips_for_month(@shown_month)
end

def show_event
@event = Event.find(params[:id])
end

def address_book
@address_books = AddressBook.paginate :all, :page => params[:page], :per_page => 35
end

def edit_my_profile
@employee = Employee.find(session[:employee_id])
end


def change_my_password
@employee = Employee.find(session[:employee_id])
if request.post?
if @employee.update_attributes(:password=>params[:employee][:password], :password_confirmation => params[:employee][:password_confirmation])
redirect_to(:action => 'edit_my_profile')
flash[:notice] = 'Password Changed'
else
render(:action => 'edit_my_profile')
flash[:notice] = 'Sorry, please try again later'
end
end
end

def write_board
@employee = Employee.find(session[:employee_id])
@write_board = WriteBoard.new(params[:write_board])
@write_boards = WriteBoard.find(:all)
end

def mail 
  
end



def create_write_board
@write_board = WriteBoard.new(params[:write_board])
@write_board.employee_id = session[:employee_id]
@write_board.ip_address = request.remote_ip
if @write_board.save
redirect_to(:action => 'write_board')
else
render(:action => 'write_board')
end
end

private

def checkclockout
@check_clock_out_attendance = Attendance.find(:first,:conditions => ["employee_id = ? and clock_in_date = ? and clock_out_date is null and clock_out_time is null", session[:employee_id],Date.today])
end


require 'date'
def days_in_month(year, month)
  (Date.new(year, 12, 31) << (12-month)).day
end

end